//
//  RecordView.swift
//  MMTest
//
//  Created by liujianlong on 2022/10/21.
//

import UIKit

class RecordView: UIView {
    @IBOutlet var forceName: UILabel!
    @IBOutlet var currentName: UILabel!
    @IBOutlet var macNum: UILabel!
    @IBOutlet var numName: UILabel!
    
    @IBOutlet var batterImg: UIImageView!
    @IBOutlet var deviceName: UILabel!
    @IBOutlet var batterlevel: UILabel!
    @IBOutlet var force: UILabel!
    @IBOutlet var forcePercent: UILabel!
    @IBOutlet var current: UILabel!
    @IBOutlet var max: UILabel!
    @IBOutlet var num: UILabel!
    var p = 0
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.loadView()
    }
    var isLeft:Bool = true{
        didSet{
            self.forceName.text = isLeft ? "左侧受力":"右侧受力"
            self.forceName.textAlignment = isLeft ? .left:.right
            self.currentName.textAlignment = isLeft ? .left:.right
            self.macNum.textAlignment = isLeft ? .left:.right
            self.numName.textAlignment = isLeft ? .left:.right
            
            self.deviceName.textAlignment = isLeft ? .left:.right
            self.force.textAlignment = isLeft ? .left:.right
            self.forcePercent.textAlignment = isLeft ? .left:.right
            self.current.textAlignment = isLeft ? .left:.right
            self.max.textAlignment = isLeft ? .left:.right
            self.num.textAlignment = isLeft ? .left:.right
        }
    }
    
    var model:SingleData = SingleData(){
        didSet{
            
            let f = String(format: "%.2f", (self.isLeft ? model.l_total_avg : model.r_total_avg) / 1000.0)
            let hex1 = self.p < 65 ? "#000000":"#FA2C2C"
            let hex2 = self.p < 65 ? "#9B9B9B":"#FA2C2C"
            self.force.attributedText = RecordView.attr(f,"kg", hex1,hex2)
            self.forcePercent.attributedText = RecordView.attr("\(self.p)","%",hex1,hex2)
            
            let batterlevel = self.isLeft ? model.l_batterlevel : model.r_batterlevel
            self.deviceName.text = self.isLeft ? "Left" : model.deviceName
            self.batterlevel.text = "\(batterlevel)%"
            self.current.attributedText = RecordView.attr("\(self.isLeft ? model.l_current : model.r_current)", "g", "#000000", "#9B9B9B")
            self.max.attributedText = RecordView.attr(String(format:"%.2f", CGFloat(self.isLeft ? model.l_max_data : model.r_max_data) / 1000.0), "kg", "#000000", "#9B9B9B")
            self.num.text = "\(model.num)"
            if batterlevel > 50{
                self.batterImg.image = UIImage(named:"电量100")
            }else if batterlevel > 10{
                self.batterImg.image = UIImage(named:"电量50")
            }else{
                self.batterImg.image = UIImage(named:"电量10")
            }
        }
    }
    
}

extension RecordView{
    class func num(_ num:Int32)->String{
        let num = NSNumber(value: num)
        let formatter = NumberFormatter()
        formatter.numberStyle = .decimal
        formatter.positiveFormat = "###,###"
        let str = formatter.string(from: num)!
        return str
    }
    
    class func attr(_ text:String , _ last:String, _ textHex:String, _ lastHex:String)->NSMutableAttributedString{
        let attr1 = NSMutableAttributedString(string:text, attributes: [.foregroundColor:textHex.color(),.font:font(20)])
        let attr2 = NSAttributedString(string:last, attributes: [.foregroundColor:lastHex.color(),.font:font(8)])
        attr1.append(attr2)
        return attr1
    }
}
